<!-- 会员编辑弹窗 -->
<template>
  <el-dialog
    width="1250px"
    :visible="visible"
    :lock-scroll="false"
    :destroy-on-close="true"
    custom-class="ele-dialog-form"
    :title="isUpdate?'修改套餐':'添加套餐'"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="132px">

  <!--    <el-form-item label="套餐背景图:">
        <uploadImage :limit="1" v-model="form.image_url"></uploadImage>
      </el-form-item> -->
      <el-form-item label="套餐类型:" prop="type">
        <el-select filterable clearable v-model="form.type" size="small" placeholder="-请选择套餐类型-"
                   class="ele-block">
          <el-option label="预约套餐" :value="1"/>
          <el-option label="续费套餐" :value="2"/>
    <!--      <el-option label="无限续时" :value="3"/> -->
        </el-select>
      </el-form-item>
      <el-form-item label="套餐名称 :" prop="name">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.name"
          placeholder="套餐名称"/>
      </el-form-item>
      <el-form-item
        label="套餐价格:"
        prop="price">
        <el-input
          :maxlength="20"
          v-model="form.price"
          placeholder="请输入套餐价格"
          clearable/>
      </el-form-item>
      <el-form-item
        label="套餐时长(单位/小时):"
        prop="time">
        <el-input
          :maxlength="20"
          v-model="form.time"
          placeholder="请输入套餐时长"
          clearable/>
      </el-form-item>

      <el-form-item label="状态:" prop="status">
        <el-radio-group
          v-model="form.status">
          <el-radio :label="1">开启</el-radio>
          <el-radio :label="2">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="场次类型:" prop="rank">
        <el-radio-group
          v-model="form.rank">
          <el-radio :label="0">普通场</el-radio>
          <el-radio :label="1">通宵场（凌晨1点至上午9点）</el-radio>
     <!--     <el-radio :label="2">上午场</el-radio> -->
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.sort"
          placeholder="排序"/>
      </el-form-item>

    </el-form>
    <div slot="footer">
      <el-button
        @click="updateVisible(false)">取消
      </el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="save">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

import uploadImage from '@/components/uploadImage'
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import TinymceEditor from '@/components/TinymceEditor';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
  name: 'MemberEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    // 栏目数据
    cateList: Array
  },
  components: {uploadImage, TinymceEditor, Treeselect},
  data() {
    return {


      // 表单数据
      form: Object.assign({status: 1}, this.data),
       form: Object.assign({rank: 0}, this.data),
      // 表单验证规则
      rules: {
        type: [
          {required: true, message: '请选择服务模式', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 会员等级列表

      labelList: [],


      is_time: 1,

    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.type = this.data.type;
        this.isUpdate = true;
      } else {
        this.form = {};
        this.form.is_time = 1;
        this.isUpdate = false;
      }
    }
  },


  mounted() {
    // 获取职级列表
    this.getDoorList();


  },
  methods: {


    /* 保存编辑 */
    save() {

      // if(this.form.time>3&&this.form.price<29.9){
      //   this.loading = false;
      //   this.$message.error('4小时及以上价格必须大于29.9元');
      //   return false;
      // }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          // 城市数据处理

          this.$http.post('/package/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },

    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },


    getDoorList() {
      this.$http.get('/course/getDoorList').then(res => {
        if (res.data.code === 0) {
          this.labelList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
</style>
